home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Function.php < prev    next >
PHP Script  |  2004-10-01  |  5KB  |  159 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Function.php,v 1.6 2003/01/04 11:54:45 mj Exp $
  19.  
  20. require_once 'Cache.php';
  21.  
  22. /**
  23. * Function_Cache
  24. *
  25. * Purpose:
  26. *
  27. *   Caching the result and output of functions.
  28. *
  29. * Example:
  30. *
  31. *   require_once 'Cache/Function.php';
  32. *
  33. *   class foo {
  34. *     function bar($test) {
  35. *       echo "foo::bar($test)<br>";
  36. *     }
  37. *   }
  38. *
  39. *   class bar {
  40. *     function foobar($object) {
  41. *       echo '$'.$object.'->foobar('.$object.')<br>';
  42. *     }
  43. *   }
  44. *
  45. *   $bar = new bar;
  46. *
  47. *   function foobar() {
  48. *     echo 'foobar()';
  49. *   }
  50. *
  51. *   $cache = new Cache_Function();
  52. *
  53. *   $cache->call('foo::bar', 'test');
  54. *   $cache->call('bar->foobar', 'bar');
  55. *   $cache->call('foobar');
  56. *
  57. * Note:
  58. *   You cannot cache every function. You should only cache 
  59. *   functions that only depend on their arguments and don't use
  60. *   global or static variables, don't rely on database queries or 
  61. *   files, and so on.
  62. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  63. * @module       Function_Cache
  64. * @modulegroup  Function_Cache
  65. * @package      Cache
  66. * @version      $Revision: 1.6 $
  67. * @access       public
  68. */
  69. class Cache_Function extends Cache {
  70.     var $expires;
  71.  
  72.     /**
  73.     * Constructor
  74.     *
  75.     * @param    string  Name of container class
  76.     * @param    array   Array with container class options
  77.     * @param    integer Number of seconds for which to cache
  78.     */
  79.     function Cache_Function($container  = 'file',
  80.                             $container_options = array('cache_dir'       => '.',
  81.                                                        'filename_prefix' => 'cache_'
  82.                                                       ),
  83.                             $expires = 3600
  84.                            )
  85.     {
  86.       $this->Cache($container, $container_options);
  87.       $this->expires = $expires;      
  88.     }
  89.  
  90.     /**
  91.     * PEAR-Deconstructor
  92.     * Call deconstructor of parent
  93.     */
  94.     function _Cache_Function() {
  95.         $this->_Cache();
  96.     }
  97.  
  98.     /**
  99.     * Calls a cacheable function or method.
  100.     *
  101.     * @return mixed $result
  102.     * @access public
  103.     */
  104.     function call() {
  105.         // get arguments
  106.         $arguments = func_get_args();
  107.  
  108.         // generate cache id
  109.         $id = md5(serialize($arguments));
  110.  
  111.         // query cache
  112.         $cached_object = $this->get($id, 'function_cache');
  113.  
  114.         if ($cached_object != NULL) {
  115.             // cache hit: return cached output and result
  116.  
  117.             $output = $cached_object[0];
  118.             $result = $cached_object[1];
  119.  
  120.         } else {
  121.             // cache miss: call function, store output and result in cache
  122.  
  123.             ob_start();
  124.             $target = array_shift($arguments);
  125.  
  126.             // classname::staticMethod
  127.             if (strstr($target, '::')) {
  128.                 list($class, $method) = explode('::', $target);
  129.  
  130.                 $result = call_user_func_array(array($class, $method), $arguments);
  131.             }
  132.  
  133.             // object->method
  134.             elseif (strstr($target, '->')) {
  135.                 list($object, $method) = explode('->', $target);
  136.                 global $$object;
  137.  
  138.                 $result = call_user_func_array(array($$object, $method), $arguments);
  139.             }
  140.  
  141.             // function
  142.             else {
  143.                 $result = call_user_func_array($target, $arguments);
  144.             }
  145.  
  146.             $output = ob_get_contents();
  147.             ob_end_clean();
  148.  
  149.             $this->save($id, array($output, $result), $this->expires, 'function_cache');
  150.         }
  151.  
  152.         echo $output;
  153.         return $result;
  154.     }
  155. }
  156. ?>
  157.